﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using CARMSData.StoredProcedureCallerClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace CARMSBusiness.Task
{
    public class ReportStatusTask :TaskBase
    {

        public override void Run()
        {
            ChangeReportTaskByG132();
        }


//G132-1	varchar	16	N	流程功能
//G132-2	varchar	4	N	執行順序
//G132-3	varchar	50	Y	進入條件
//G132-4	varchar	50	Y	離開條件
//G132-5	varchar	20	N	進入表單群
//G132-6	varchar	50	Y	跳躍條件
//G132-7	varchar	4	N	跳躍(流程功能)



        /// <summary>
        /// 定时执行 ,
        /// 根据g132中的 进入条件和 离开条件
        /// G130-8				进入表单 事件
        /// 
        /// 
        /// g132-->g132进入条件 -->G132-5 进入表单群--> report category的 g130-8 进入表单事件
        /// g132-->g132离开条件 -->   没有下文
        /// 
        /// 
        /// step 1 : 遍历 g132中的进入条件,查找对应表单 所在的origin report number,
        /// step 2 : 查找出来的结果表示 这些report 满足 g132-3 的进入条件,那么这些对应的 report flow 状态变更为 此 g132状态,并且执行 关联表单群的 事件
        /// </summary>
        private void ChangeReportTaskByG132()
        {
            var g132collection = new G132Collection();
            g132collection.GetMulti(null);
            foreach (var g132Entity in g132collection)
            {
                GenerateStartMessage(g132Entity);
                GenerateOverMessage(g132Entity);
            }

        }

        private static void GenerateStartMessage(G132Entity g132Entity)
        {
             if(g132Entity.G1323.Trim().Length>0)
                {
                    var listInConditions = DatabaseSp.ExcuteSp(g132Entity.G1323.Trim(), string.Empty, string.Empty);
                    var listOriginReportNumber = new List<string>();
                    foreach (DataRow row in listInConditions.Rows)// step 1 
                    {
                        if( row["ReportNumber"]!=null)
                        {
                            listOriginReportNumber.Add(row["ReportNumber"].ToString());
                        }
                    }

                    //step 2 ,search reprot flows,查找合适的flow,并根据g132的进入表单群 G132-5 ,根据这些表单群得进入消息,产生消息 task 
                    var reportFlowCollection = new ReportFlowCollection();
                    var reportFlowCondition = new PredicateExpression();
                    var reportNumberField = new FieldCompareRangePredicate(ReportFlowFields.OrginReportNumber, false,
                                                                           listOriginReportNumber);
                    reportFlowCondition.Add(reportNumberField);
                    reportFlowCondition.Add(ReportFlowFields.FlowStatus ==
                                            DictionaryCategoryConst.FlowStatus.FlowCompleted);


                    reportFlowCollection.GetMulti(reportFlowCondition);

                    //stp 3 分别更新 report flow 状态,并产生 report 触发消息
                    foreach (var flowEntity in reportFlowCollection)
                    {
                        flowEntity.FlowStepId = g132Entity.Gid;
                        flowEntity.FlowStatus = DictionaryCategoryConst.FlowStatus.InCurrentFlow;
                        flowEntity.Save(); 
                        //
                        if (flowEntity.ReportHeader.Count>0)  //发送报表进入消息
                        {
                            GFlowBusiness.GenerateReportInFlowTask(flowEntity.ReportHeader[0], "System");
                        }
                    } 
 
                }
        }
        /// <summary>
        /// 
        /// </summary>
        /// G132-4	varchar	50	Y	離開條件
        /// <param name="g132Entity"></param>
        private static void GenerateOverMessage(G132Entity g132Entity)
        {
            if (g132Entity.G1324.Trim().Length > 0)
            {
                var listInConditions = DatabaseSp.ExcuteSp(g132Entity.G1324.Trim(), string.Empty, string.Empty);
                var listOriginReportNumber = new List<string>();
                foreach (DataRow row in listInConditions.Rows)// step 1 
                {
                    if (row["ReportNumber"] != null)
                    {
                        listOriginReportNumber.Add(row["ReportNumber"].ToString());
                    }
                }

                //step 2 ,search reprot flows,查找合适的flow,并根据g132的进入表单群 G132-5 ,根据这些表单群得进入消息,产生消息 task 
                var reportFlowCollection = new ReportFlowCollection();
                var reportFlowCondition = new PredicateExpression();
                var reportNumberField = new FieldCompareRangePredicate(ReportFlowFields.OrginReportNumber, false,
                                                                       listOriginReportNumber);
                reportFlowCondition.Add(reportNumberField);
                reportFlowCondition.Add(ReportFlowFields.FlowStatus ==
                                        DictionaryCategoryConst.FlowStatus.InCurrentFlow);


                reportFlowCollection.GetMulti(reportFlowCondition);

                //stp 3 分别更新 report flow 状态,并产生 report 触发消息
                foreach (var flowEntity in reportFlowCollection)
                { 
                    flowEntity.FlowStatus = DictionaryCategoryConst.FlowStatus.FlowCompleted;
                    flowEntity.Save();
             
                }

            }
        }
    }
}
